window.onload = function(){
    let adddButton = document.getElementById('addButton');
    const form = document.getElementById('addMovie');

    adddButton.addEventListener('click',e=>{
        //阻止默认的表单提交时间，这样页面就不会再自己提交和刷新
        e.preventDefault();

        const movieData = getMovieData('addMovie');
        console.log(movieData);

        axios.post(' http://localhost:3000/movies',movieData)
        .then(function(response){
            if(response.data){
                alert('添加成功！');
                form.reset();
            }
        })
        .catch(function(error){
            console.log(error);
        });
    },false);
}

//拿到输入的数据
function getMovieData(formId){
    const form = document.getElementById(formId);
    let tagElements = form.getElementsByTagName('input');
    let movieData = {};

    for(let item of tagElements){
        movieData[item.name] = item.value;
    }

    return movieData;
}